<?php
/************************************************************************
 Omnitrix - PHP Development Framework
 Copyright (C) 2012-2018 amonest(eastson@outlook.com)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
************************************************************************/

function otx_theme_name($theme_name = null)
{
	if (null === $theme_name) {
		return otx_param_string('_theme_name');
	}
	otx_param_set('_theme_name', $theme_name);
}

// function otx_theme_render()
// {
// 	$vars['content'] = otx_param_string('_content');
// 	$dirs[] = DIR_THEMES . otx_theme_name() . '/templates';
// 	return otx_template_parse('index', $vars, $dirs);
// }

function otx_theme_url($theme_name, $sub_path = null)
{
	$theme_path = HTTP_THEMES . $theme_name . '/';

	if (empty($sub_path)) {
		return $theme_path;
	} else {
		return $theme_path . $sub_path;
	}
}

function otx_theme_path($theme_name, $sub_path = null)
{
	$theme_path = DIR_THEMES . $theme_name . DS;

	if (empty($sub_path)) {
		return $theme_path;
	} else {
		return $theme_path . $sub_path;
	}
}

function otx_theme_instance($theme_name)
{
	static $themes = null;

	if (null === $themes) {
		$themes = otx_cache_fetch('themes');
		if (false === $themes) {
			$themes = array();

			$sql = 'SELECT * FROM #system_theme#';
			$rows = otx_db_fetch_all($sql);

			if ($rows) {
				foreach ($rows as $row) {
					$themes[$row['theme_name']] = $row;
				}
			}

			otx_cache_write('themes', $themes);
		}
	}

	if (isset($themes[$theme_name]) && isset($themes[$theme_name]['instance'])) {
		return $themes[$theme_name]['instance'];
	}

	$theme_path = otx_theme_path($theme_name);
	if (!is_dir($theme_path)) {
		throw new Exception('Theme directory not exists: ' . $theme_path);
	}

	$theme_file = $theme_path . DS. $theme_name . '.php';
	if (!is_file($theme_file)) {
		throw new Exception('Theme file not exists: ' . $theme_file);
	}

	require_once $theme_file;

	$theme_class = $theme_name . '_theme';
	if (!class_exists($theme_class)) {
		throw new Exception('Theme class not exists: ' . $theme_class);
	}

	$instance = new $theme_class();
	$instance->theme_name = $theme_name;
	$instance->theme_path = $theme_path;

	if (isset($themes[$theme_name])) {
		$instance->theme_id = $themes[$theme_name]['theme_id'];
	}

	$themes[$theme_name]['instance'] = $instance;
	return $instance;
}

function otx_theme_info($theme_name)
{
	if (is_string($theme_name)) {
		$instance = otx_theme_instance($theme_name);
	} else {
		$instance = $theme_name;
	}

	$info = array(
			'name' => $instance->theme_name,
			'title' => $instance->theme_name,
			'description' => null,
			'website' => null,
			'email' => null,
			'author' => null,
			'version' => null
	);

	return array_merge($info, $instance->get_info());
}